use strict;
use warnings;

package Gravity::Model::Tag;
use Jifty::DBI::Schema;

use Gravity::Record schema {

    column name =>
        type is 'text',
        label is 'Name',
        is mandatory,
        is distinct,
    ;

};

sub since { '0.0.4' }

sub canonicalize_name {
    my ( $self, $name ) = @_;

    # trim
    $name =~ s{ \A \s+ }{}xms;
    $name =~ s{ \s+ \z }{}xms;

    # spaces collapsed into underscore
    $name =~ tr{ }{_}s;

    # get rid of all but alphanum, space, underscore
    $name =~ s{ [^[:word:]] }{}xmsg;

    # trim trailing underscores
    $name =~ s{ [_]+ \z}{}xms;

    # tags should always start with a hash sign
    if ( $name !~ m{ \A [#] }xms ) {
        $name = '#'.$name;
    }

    # lower case,please
    return lc $name
}

1;

